<template>
  <div class="login_box">
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-width="80px"  
      class="login-box"
    >
      <h2>欢迎使用</h2>
      <el-form-item label="用户名" prop="username" class="login-title">
        <el-input v-model="ruleForm.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="ruleForm.password"
          autocomplete="off"
          type="password"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          style="width: 400px"
          type="primary"
          @click="submitForm(ruleFormRef)"
          >登录</el-button
        >
      </el-form-item>
      <span
        >没有账号?请先去
        <router-link to="/register"> 注册</router-link>一下</span
      >
    </el-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref, toRefs } from "vue";
import type { FormInstance } from "element-plus";
import { LoginData } from "../type/login";
import { login } from "../request/api";
import { useRouter } from "vue-router";
// import {RoleListInt,RoleInitData} from '../type/role'

export default defineComponent({
  setup() {
    const data = reactive(new LoginData());
    const rules = {
      username: [
        {
          required: true,
          trigger: "blur",
          message: "请输入用户名",
        },
        {
          min: 3,
          max: 10,
          trigger: "blur",
        },
      ],
      password: [
        {
          required: true,
          message: "请输入密码",
          trigger: "blur",
        },
        {
          min: 3,
          max: 10,
          // message: "密码长度应该在3-10之间",
          trigger: "blur",
          // pattern:
          //   "^(?=.*\\d)(?=.*[a-zA-Z])(?=.*[!@#$%*--+/\\\\.])[\\da-zA-Z!@#$%*--+/\\\\.]{6,}$",
        },
      ],
    };
    
    const ruleFormRef = ref<FormInstance>();
    const router = useRouter(); //相当于$router
    const submitForm = (formEl: FormInstance | undefined) => {
      if (!formEl) return;
      //对表单的内容进行验证
      //v e验证成功，反之失败
      formEl.validate((valid) => {
        if (valid) {
          // console.log('submit!')
          login(data.ruleForm).then((res) => {
            console.log(res);
            //将token进行保存
            localStorage.setItem("token", res.data.token);
            //跳转首页
            router.push("/");
          });
        } else {
          
          console.log("error submit!");
          return false;
        }
      });
    };
    //重置
    /*  const resetForm=()=>{
               data.ruleForm.username='',
               data.ruleForm.password=''
            } */
    const resetForm = (formEl: FormInstance | undefined) => {
      if (!formEl) return;
      formEl.resetFields();
    };
    return { ...toRefs(data), rules, ruleFormRef, submitForm, resetForm, };
  },
});
</script>

<style lange="scss" scoped>
.login_box {
  width: 100%;
  min-height: 100vh;
  background-image: url("../assets/login1.jpg");
  overflow: hidden;
  /* background-repeat: repeat; */
  background-size: cover;
}
h2 {
  color: #106a9b;
  text-align: center;
  margin-bottom: 30px;
}
.login-box {
  width: 400px;
  margin: 120px auto;
  border: 1px solid #dcdfe6;
  background-color: white;
  opacity: 0.9;
  padding: 40px;
  border-radius: 10px;
  box-shadow: 0 0 10px #dcdfe6;
}
span {
  margin-left: 120px;
}
.login-title {
  /* margin-top: 20px; */
  text-align: center;
  color: #79bbff;
}
</style>
